//
//  YLRegularFinancingViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/13.
//  Copyright © 2017年 she1323. All rights reserved.
//  定期理财

#import "YLRegularFinancingViewModel.h"
#import "YLGlobeHeader.h"
#import <UIKit/UIKit.h>

#import "WHNetworkTool.h"
#import "WHProgressHUD.h"

#import "YLAccountTool.h"
#import "YLRegularFinancingProgressModel.h"
#import "YLProgressHeaderModel.h"
#import <YYModel.h>

@implementation YLRegularFinancingViewModel

- (NSMutableArray *)models
{
    if (!_models) {
        _models = [NSMutableArray arrayWithCapacity:2];
    }
    return _models;
}

- (RACCommand *)progressCommand
{
    if (!_progressCommand) {
        __weak typeof(self) weakSelf = self;
        _progressCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
           
            NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetRegularFinancing",YLURL];
            
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                    YLRegularFinancingProgressModel *progressModel = [YLRegularFinancingProgressModel yy_modelWithDictionary:responseObject[@"httpResult"]];
                
                    // 第一个
                    {
                        YLProgressHeaderGroup *group = [[YLProgressHeaderGroup alloc] init];
                        {
                            YLProgressHeaderModel *model = [[YLProgressHeaderModel alloc] init];
                            model.title = @"待收本金";
                            model.content = [NSString stringWithFormat:@"%.2f", progressModel.noPayCurrentPrincipalTotal];
                            model.circleColor = YLColor(215, 60, 49);
                            [group.group addObject:model];
                        }
                        {
                            YLProgressHeaderModel *model = [[YLProgressHeaderModel alloc] init];
                            model.title = @"已收本金";
                            model.content = [NSString stringWithFormat:@"%.2f", progressModel.currentPrincipalTotal];
                            model.circleColor = YLColor(246, 180, 63);
                            [group.group addObject:model];
                        }
                        
                        group.progress = progressModel.currentPrincipalTotal / (progressModel.noPayCurrentPrincipalTotal + progressModel.currentPrincipalTotal);
                        
                        group.backgroundStrokeColor = YLColor(215, 60, 49);
                        group.progressStrokeColor = YLColor(246, 180, 63);
                        
                        [weakSelf.models addObject:group];
                    }
                    
                    // 第二个
                    {
                        YLProgressHeaderGroup *group = [[YLProgressHeaderGroup alloc] init];
                        {
                            YLProgressHeaderModel *model = [[YLProgressHeaderModel alloc] init];
                            model.title = @"待收收益";
                            model.content = [NSString stringWithFormat:@"%.2f", progressModel.noPayIncomeAmountTotal];
                            model.circleColor = YLColor(191, 34, 51);
                            [group.group addObject:model];
                        }
                        {
                            YLProgressHeaderModel *model = [[YLProgressHeaderModel alloc] init];
                            model.title = @"已收收益";
                            model.content = [NSString stringWithFormat:@"%.2f", progressModel.incomeAmountTotal];
                            model.circleColor = YLColor(253, 112, 169);
                            [group.group addObject:model];
                        }
                        
                        group.progress = progressModel.incomeAmountTotal / (progressModel.incomeAmountTotal + progressModel.noPayIncomeAmountTotal);
                        group.backgroundStrokeColor = YLColor(191, 34, 51);
                        group.progressStrokeColor = YLColor(253, 112, 169);
                        [weakSelf.models addObject:group];
                    }

                    [subscriber sendNext:nil];
                } else {
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
                
                [subscriber sendCompleted];
                
                
            } fail:^(NSError *error) {
                [subscriber sendCompleted];
            }];
            
            return nil;
            
        }];
            
        
        }];
    }
    return _progressCommand;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}

@end
